﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.Administration.AccessControl;
using Microsoft.SharePoint.Taxonomy;
using System.Xml.Linq;
using System.Globalization;

namespace SPMetadataManager.Taxonomy
{
    public class Importer
    {
        public void ImportTermStore(TermStoreCollection termStoreCollection, XElement termStoreElement, bool recurse = true)
        {
            try
            {
                //Get the term store name
                string termStoreName = (string)termStoreElement.Attribute("Name");
                
                //Check if term store exists
                TermStore termStore = (from termstore in termStoreCollection where termstore.Name == termStoreName select termstore).SingleOrDefault();

                //TODO: If the term store is not there, create it
                if(termStore == null)
                {
                    throw new Exception("Term Store not found. Please create the term store from central admin"); 
                }

                if (recurse)
                {
                    foreach (XElement childElement in termStoreElement.Elements())
                    {
                        ImportGroup(termStore, childElement, recurse);
                    }
                }

                termStore.CommitAll();
            }
            catch(Exception e)
            {
            }
           
        }

        public void ImportGroup(TermStore termStore, XElement groupElement, bool recurse = true)
        {
            //Get the group name
            string groupName = (string)groupElement.Attribute("Name");

            //Check if the group exists
            Group group = (from termgroup in termStore.Groups where termgroup.Name == groupName select termgroup).SingleOrDefault();

            //If it doesn't exist, create it
            if (group == null)
            {
                group = termStore.CreateGroup(groupName);
            }

            //Create all child term sets in the group
            if (recurse)
            {
                foreach (XElement childElement in groupElement.Elements())
                {
                    ImportTermSet(group, childElement, recurse);
                }
            }
        }

        public void ImportTermSet(Group termGroup, XElement termSetElement, bool recurse = true)
        {
            try
            {
                //Get the termset name
                string termSetName = (string)termSetElement.Attribute("Name");

                //Check if the termset exists
                TermSet termSet = (from termset in termGroup.TermSets where termset.Name == termSetName select termset).SingleOrDefault();

                //If it doesn't exist, create it
                if (termSet == null)
                {
                    termSet = termGroup.CreateTermSet(termSetName);
                }

                //Create all the child terms in the termset
                if (recurse)
                {
                    foreach (XElement childElement in termSetElement.Elements())
                    {
                        ImportTerm(termSet, childElement, recurse);
                    }
                }
            }
            catch (Exception e)
            {

            }
        }

        public void ImportTerm(TermSet termSet, XElement termElement, bool recurse = true)
        {
            try
            {
                //Get the term name
                string termName = (string)termElement.Attribute("Name");

                //Check if the term exist
                Term term = termSet.GetTerms(termName, false).FirstOrDefault();

                //If the term does not exist, create it
                if (term == null)
                {
                    int lcid = CultureInfo.CurrentCulture.LCID;
                    term = termSet.CreateTerm(termName, lcid);
                }

                //Create the child terms
                if (recurse)
                {
                    foreach (XElement childElement in termElement.Elements())
                    {
                        ImportTerm(term, childElement, recurse);
                    }
                }
            }
            catch (Exception e)
            {
            }

        }

        public void ImportTerm(Term parentTerm, XElement termElement, bool recurse = true)
        {
            try
            {

                //Get the LCID.
                //TODO: Support importing LCIDs
                int lcid = CultureInfo.CurrentCulture.LCID;

                //Get the term name
                string termName = (string)termElement.Attribute("Name");


                //Check if the term exist
                Term term = parentTerm.GetTerms(termName, lcid, true, StringMatchOption.ExactMatch, 1, false).FirstOrDefault();

                //If the term does not exist, create it
                if (term == null)
                {
                    term = parentTerm.CreateTerm(termName, lcid);
                }

                //Create the child terms
                if (recurse)
                {
                    foreach (XElement childElement in termElement.Elements())
                    {
                        ImportTerm(term, childElement, recurse);
                    }
                }
            }
            catch (Exception e)
            {
            }
        }
    }
}
